package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CheckItemController
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:03
 * @Version V1.0
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    // dubbo消费者
    @Reference
    private MenuService menuService;

    /**
     * 根据登陆的用户名信息查询该用户所对应的菜单
     * @param username
     * @return
     */
    @RequestMapping("/findMenuListByUsername")
    public Result findMenuListByUsername(String username) {
        try {
            List<Menu> menuList = menuService.findMenuListByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    //新增菜单
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu) {
        try {//添加成功
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {//添加失败
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    //分页查询菜单列表
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult =  menuService.findPage(queryPageBean);
        return pageResult;
    }

    //删除指定id的菜单
    @RequestMapping("delete")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    //根据id查询菜单信息，用于编辑菜单回显
    @RequestMapping("findById")
    public Result findById(Integer id) {
        try {
            Menu menu =  menuService.findById(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUCCESS ,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    //编辑菜单
    @RequestMapping("update")
    public Result updateCheckItem(@RequestBody Menu menu) {
        try {
            menuService.updateMenu(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }
    //查询所有菜单
    @RequestMapping("findAll")
    public List<Menu> findAll() {
        return menuService.findAll();
    }
}
